package manager.http;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import advice.ExceptionManager;


/**
 * 类名：GetPayQRCode
 * 描述：和php后台通信的httpget请求
 * @author zk
 * @version 0.0.1
 * @date 2018-4-18
 *
 */

public class HttpClient {
	private static Logger logger = Logger.getLogger(ExceptionManager.class);
	
	public String httpGet(URI uri) throws ParseException, IOException   {
		
		// 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        
        HttpGet httpGet = new HttpGet(uri);
        //response 对象
        CloseableHttpResponse response = null;
        try {
            // 执行http get请求
            try {
				response = httpclient.execute(httpGet);
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                //System.out.println("php后台返回的数据："+content);
                logger.info("php retrun:"+"\n"+content);
                return content;
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
		
		return null;
	}
}